<template>
    <!-- 面板头部 -->
    <panelHead :route="route" />
        <!-- 搜索 -->
    <div class="form">
        <el-form :model="searchForm" :inline="true">
            <el-form-item prop="out_trade_no">
                <el-input v-model="searchForm.out_trade_no" placeholder="请输入订单号" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
        </el-form>
    </div>
    <!-- 订单表格 -->
    <el-table :data="tableData.list" style="width: 100%">
        <!-- fixed="left" 固定左侧 -->
        <el-table-column prop="out_trade_no" label="订单号" width="150px" fixed="left" />
        <el-table-column prop="hospital_name" label="就诊医院" width="120px" />
        <el-table-column prop="service_name" label="陪诊服务" width="150px" />
        <el-table-column label="陪护师">
            <template #default="scope">
                <el-image :src="scope.row.companion.avatar" style="width: 40px; height: 40px; border-radius: 50%"
                    alt="" />
            </template>
        </el-table-column>
        <el-table-column prop="companion.mobile" label="陪护师手机号" width="150px" />
        <el-table-column prop="price" label="总价" />
        <el-table-column prop="paidPrice" label="已付" />
        
        <el-table-column label="下单时间" width="120px">
            <template #default="scope">
                {{ dayjs(scope.row.order_start_time).format('YYYY-MM-DD') }}
            </template>
        </el-table-column>

        <el-table-column label="订单状态">
            <template #default="scope">
                <el-tag :type="statusMap(scope.row.trade_state)">
                    {{ scope.row.trade_state }}
                </el-tag>
            </template>
        </el-table-column>

        <el-table-column prop="service_state" label="接单状态" />
        <el-table-column prop="client.mobile" label="联系人手机号" width="150px" />
            
               <!-- fixed="left" 固定右侧 -->
        <el-table-column label="操作" fixed="right">
            <template #default="scope">
            
                <!-- 服务完成弹窗 -->
                <el-popconfirm v-if="scope.row.trade_state === '待服务'" confirm-button-text="是" cancel-button-text="否"
                    cancel-button-type="info" :icon="InfoFilled" icon-color="#626AEF" title="是否确认已完成服务?"
                    @confirm="confirmEvent">
                        
                    <!-- 服务完成按钮 -->
                    <template #reference>
                        <el-button type="primary" link>服务完成</el-button>
                    </template>
                </el-popconfirm>

                <!-- 暂无服务 -->
                <el-button v-else type="primary" link disabled>暂无服务</el-button>
            </template>
        </el-table-column>

    </el-table>
        <!-- 分页组件 -->
     <div class="pagination-info">
         <el-pagination
      v-model:current-page="paginationData.pageNum"
      :page-size=" paginationData.pageSize"
      :background="false"
      size="small"
      layout="total, prev, pager, next"
      :total="tableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
         />
     </div>
</template>

<script setup name="Order">
import { onMounted, reactive } from 'vue'
import { useRoute } from 'vue-router'
import { adminOrdert, updateOrder } from '../../../api'
import { InfoFilled } from '@element-plus/icons-vue'
import dayjs from 'dayjs' // 时间格式化

const route = useRoute()

// 搜索表单
const searchForm = reactive({
    out_trade_no: ''
})
// 查询
const onSubmit = () => {
    getOrdertList(searchForm)
}

// 订单状态
const statusMap = (state) => {
    const status = {
        '待服务': 'primary',
        '待支付': 'warning',
        '已完成': 'success',
        '已取消': 'info'
    }
    return status[state]
}
// 服务完成确认
const confirmEvent = (id) => {
    // 更新后端订单数据中 订单状态为已完成
    updateOrder({ id }).then(({ data }) => {
        console.log(data)
        if (data.code === 10000) {
            getOrdertList()
        }
    })
}
// 订单列表数据
const tableData = reactive({
    list: [],
    total: 0
})
const paginationData = reactive({
    pageNum: 1,
    pageSize: 10
})
onMounted(() => {
    // 获取订单列表
    getOrdertList()
})

// 获取订单列表
const getOrdertList = (params) => {
    //合并
    adminOrdert({ ...paginationData, ...params }).then(({ data }) => {
        // 将获取到的数据赋值给tableData
        const { list, total } = data.data
        tableData.list = list
        tableData.total = total
    })
}
const handleSizeChange = (val) => {
    paginationData.pageSize = val
    getListData()
}
const handleCurrentChange = (val) => {
    paginationData.pageNum = val
    getListData()
}
</script>

<style lang="less" scoped>
.form {
    display: flex;
    justify-content: flex-end;
    padding: 10px 0 10px 10px;
    background-color: #fff;
}
</style>